<title>后台管理员</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>订单管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
      <div class="layui-form-item">

        <div class="layui-inline">
          <label class="layui-form-label">新闻标题</label>
          <div class="layui-input-block">
            <input type="text" name="newsTitle" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <label class="layui-form-label">新闻预览</label>
          <div class="layui-input-block">
            <input type="text" name="newsPreview" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <label class="layui-form-label">新闻类型</label>
          <div class="layui-input-block">
            <input type="text" name="newsType" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <label class="layui-form-label">新闻预览</label>
          <div class="layui-input-block">
            <input type="text" name="isHot" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>

    <!-- <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>上传文件</button> -->

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <!-- <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button> -->
        <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
      </div>

      <table class="layui-hide" id="table-instance" lay-filter="table-instance"></table>

      <script type="text/html" id="table-useradmin-admin">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table', 'laydate', 'upload'], function () {
    var $ = layui.$
      , admin = layui.admin
      , setter = layui.setter
      , view = layui.view
      , table = layui.table
      , form = layui.form
      , laydate = layui.laydate
      , upload = layui.upload;

    form.render(null, 'layadmin-useradmin-formlist');

    //监听搜索
    form.on('submit(LAY-user-back-search)', function (data) {
      var field = data.field;

      //执行重载
      table.reload('table-instance', {
        where: field
      });
    });

    //指定允许上传的文件类型
    upload.render({
      elem: '#test3'
      , url: setter.request.baseUrl + "/orderform/import" //改成您自己的上传接口
      , accept: 'file' //普通文件
      , done: function (res) {
        layer.msg('上传成功');
        console.log(res);
      }
    });

    table.render({
      elem: "#table-instance",
      url: setter.request.baseUrl + "/news/admin/search",
      method: "get",
      headers: { token: layui.data("layuiAdmin").token },
      response: {
        statusName: "code",
        statusCode: 0,
        msgName: "msg",
        countName: "count",
        dataName: "list",
      },
      cols: [
        [
          { field: "newsId", width: "5%", title: "ID", sort: true },
          { field: "newsTitle", width: "10%", title: "新闻标题" },
          { field: "newsImg", title: "订单号" },
          {
            field: "newsTime", title: "时间", width: "10%", templet: function (d) {
              var star_addtime = layui.util.toDateString(new Date(d.newsTime * 1000).getTime(), "yyyy/MM/dd HH:mm:ss");
              return '<div>' + star_addtime + '</div>'
            }
          },
          { field: "newsPreview", title: "预览" },
          { field: "isHot", title: "交易状态", templet: "#ReviewState" },
          { field: "newsContent", title: "详情" },
          { field: "readCount", title: "阅读数量" },
          { field: "newsType", title: "新闻类型", templet: "#MailStatus" },

          // templet:"#Sex"
          { fixed: "right", title: "操作", toolbar: "#table-useradmin-admin", width: 150 },
        ],
      ],
      page: true,
    });

    //修改模块
    table.on("tool(table-instance)", function (obj) {
      var data = obj.data;

      if (obj.event === "del") {
        layer.confirm("真的删除么", function (index) {
          delAdminHandle({
            orderFormId: data.orderFormId,
          });

          obj.del();
          layer.close(index);
        });
      } else if (obj.event === "edit") {
        admin.popup({
          title: "编辑用户",
          area: ["500px", "500px"],
          success: function (layero, index) {

            view(this.id)
              .render("orderform/orderform", data)
              .done(function () {
                let _data = data;
                form.render(null, "layuiadmin-form-admin");

                form.on("submit(LAY-user-front-submit)", function (data) {

                  var field = data.field;

                  editUserHandle(Object.assign(_data, field));

                  layer.close(index);
                });
              });
          },
        });
      }
    });

    //修改编辑
    function editUserHandle(params) {
      admin.req({
        url: setter.request.baseUrl + "/orderform/admin/update",
        data: JSON.stringify(params),
        type: "post",
        dataType: "json"
        , contentType: "application/json",
        done: function (res) {
          layui.table.reload("table-instance");
        },
      });
    }

    //删除
    function delAdminHandle(params) {
      console.log(params)

      //  debugger
      admin.req({
        //*****************需要替换****************
        url: setter.request.baseUrl + "/orderform/admin/delete",
        data: {
          ...params,
        },
        type: "post",
        done: function (res) {
          layer.msg(res.msg);
        },
      });
    }

    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>


<script type="text/html" id="ReviewState">
  {{#  if(d.isHot == 0){ }}
  {{# return '非热门' }}
  {{#  } }}

  {{#  if(d.isHot == 1){ }}
    {{# return '热门' }}
  {{#  } }}

</script>